package com.zixuan.sina.config.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 直连交换机
 */
@Slf4j

public class DirectChange {
    private String exchangeName;
    private String queueName;

    public DirectChange(String exchangeName, String queueName) {
        this.exchangeName = exchangeName;
        this.queueName = queueName;
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            String EXCHANGE_NAME = exchangeName;
            channel.exchangeDeclare(EXCHANGE_NAME, "direct");

            // 创建队列，随机分配一个队列名称
            channel.queueDeclare(queueName, true, false, false, null);
            channel.queueBind(queueName, EXCHANGE_NAME, "my_routingKey");
            log.info("队列创建成功");
        } catch (Exception e) {
            log.error("队列创建失败");
        }

    }

    public static void doInit(){
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        String EXCHANGE_NAME = "my_change";
        channel.exchangeDeclare(EXCHANGE_NAME, "direct");

        // 创建队列，随机分配一个队列名称
        String queueName = "my_queue";

        channel.queueDeclare(queueName, true, false, false, null);
        channel.queueBind(queueName, EXCHANGE_NAME, "my_routingKey");
        log.info("队列创建成功");
    } catch (Exception e) {
        log.error("队列创建失败");
    }
}
}
